<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/5/25
 * Time: 19:04
 */

namespace App\Models\Traits;
use Carbon\Carbon;
use App\Models\Goods;



trait GoodsHandler
{

    public function sumShopIdGoodsBrowseTrait($exclude = [])
    {
        return $this->goodsBrowse()->when('goodsBrowse' , function($query) use ($exclude){
            $query->select([
                'id',
                'user_id',
                'shop_id',
                'goods_id',
                \DB::raw('SUM(pagevies) as pagevies'),
                \DB::raw('SUM(show_num) as show_num'),
                \DB::raw('SUM(order_num) as order_num'),
                \DB::raw('SUM(deal_num) as deal_num'),
            ])->groupBy('shop_id');
            $query->when(isset($exclude['created_at']) , function($q2) use ($exclude){
                list($t1 , $t2) = explode('/' , $exclude['created_at']);
                $q2->whereBetween('created_at',[Carbon::parse($t1) , Carbon::parse($t2)]);
            });
        });
    }


    /*
     * 获取 userBrowseTotal 数据的总合
     * $date 为要查询的时间
     */
    public function requestBrowseTotalNum()
    {
        $data['todayPagevies'] = auth()->user()->userBrowseTotal()->whereDate('created_at' , now())->sum('pagevies');
        $data['totalPagevies'] = auth()->user()->userBrowseTotal()->sum('pagevies');
        $data['todayOrderNum'] = auth()->user()->userBrowseTotal()->whereDate('created_at' , now())->sum('order_num');
        $data['totalOrderNum'] = auth()->user()->userBrowseTotal()->sum('order_num');
        $data['todayDealNum'] = auth()->user()->userBrowseTotal()->whereDate('created_at' , now())->sum('deal_num');
        $data['totalDealNum'] = auth()->user()->userBrowseTotal()->sum('deal_num');
        $data['todayShowNum'] = auth()->user()->userBrowseTotal()->whereDate('created_at' , now())->sum('show_num');
        $data['totalShowNum'] = auth()->user()->userBrowseTotal()->sum('show_num');

        return $data;
    }



    // 生成数据图表
    // $type 生成图片类型 bar 为柱型
    public function requestChart($type = 'bar'){

        $browse = auth()->user()->userBrowseTotal()->select([
            \DB::raw('SUM(pagevies) as pagevies'),
            \DB::raw('SUM(deal_num) as deal_num'),
            \DB::raw('SUM(show_num) as show_num'),
            \DB::raw('DATE_FORMAT(created_at,"%Y-%m") as date'),
        ])->groupBy('date')->get();


        if (!$browse->count()) return false;
        foreach($browse as $k=>$v){
            $res['pagevies'][] = $v->pagevies;
            $res['deal_num'][] = $v->deal_num;
            $res['show_num'][] = $v->show_num;
            $res['date'][] = $v->date;
        }

        // 生成数据
        $drawing = [
            [
                'name'=> '浏览量',
                'type'=> $type,
                'data'=> $res['pagevies'],
            ],
            [
                'name'=> '成交量',
                'type'=> $type,
                'data'=> $res['deal_num'],
            ],
            [
                'name'=> '展现量',
                'type'=> $type,
                'data'=> $res['show_num'],
            ],
        ];

        $month = json_encode($res['date'] , JSON_UNESCAPED_UNICODE);
        $drawing = json_encode($drawing , JSON_UNESCAPED_UNICODE);

        return [$month , $drawing];
    }




}